package com.chufuying.tools;
import com.chufuying.tools.beans.ColumnInfoForTable;
import com.chufuying.tools.services.BeanFactory;
import com.chufuying.tools.services.FindTables;
import com.chufuying.tools.services.impl.BeanFactoryImpl;
import com.chufuying.tools.services.impl.FindTablesImpl;
import java.sql.Connection;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 业务逻辑层
 * @date:2015-09-11
 * @author: Faizel
 * @version:1.0.2
 * */
public class Service {	
	public void service(){
		/**
		 * 加载配置文件
		 * */
		LoadDBconfig lc = new LoadDBconfig();
		lc.loadConfig();
		/**
		 * 获取表信息
		 * */		
		DataSource ds = new DataSource();
		Connection con = ds.getConn();
		try{
			FindTables ft = new FindTablesImpl();
			Map<String, List<ColumnInfoForTable>> map = ft.Find(con);
			con.close();
			if(map.size() > 0) {
				Set<String> keysets = map.keySet();
				BeanFactory bf = new BeanFactoryImpl();
				/** 调用工厂方法生成类 **/
				for (String key : keysets) {
					bf.generateClass(key,map.get(key));
				}
			}
			else{
				System.out.println("################/r/t " +
						"Sorry ! there have no any tables !" +
						"please check the database !");
			}

		}catch(Exception e){
			e.printStackTrace();
			try{
				if(con != null){
					con.close();
				}
			}
			catch(Exception e1){
				e1.printStackTrace();
			}				
		}		
		System.out.println("success!!");
	}
}
